package com.ktjy.controller;

import com.ktjy.service.UploadService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/admin/")
public class UploadController {
    Logger log= LoggerFactory.getLogger(UploadController.class);
    @Autowired
    UploadService uploadService;

    @PostMapping("/upload")
    public Map<String,Object> upload(@RequestParam("file") MultipartFile file){
        Map<String,Object> map=new HashMap<>();
        try {
            String path = uploadService.uploadFile(file);
            if(path!=null){
                map.put("code",1);
                map.put("msg","上传成功");
                map.put("data",path);
            }else{
                map.put("code",0);
                map.put("msg","上传失败");
            }
        } catch (IOException e) {
            log.error("SysUserController中accountExist方法执行报错的。",e);
            map.put("code",0);
            map.put("msg","上传失败");
        }

        return map;
    }
}
